package com.svn.operate;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashSet;
import java.util.Set;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;

public class IntelligentMatch extends JFrame{


    private JTextField keywordField;

    private JPopupMenu popupMenu;
    private JList cacheKeywordList;
    private Set<String> cacheKeyword = new HashSet<String>();
    private Set<String> matchResultSet = new HashSet<String>();
    private ResultListModel resultListModel;
    private ListListener listListener = new ListListener();
    
    public IntelligentMatch(){
        super();
        init();
        
    }
    
    public void init(){
        keywordField = new JTextField(22);
        Listener listener = new Listener();
        KeyDisposer keyListener = new KeyDisposer();
        keywordField.registerKeyboardAction(listener,KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),JComponent.WHEN_FOCUSED);
        
        JButton searchButton = new JButton("Search");
        searchButton.addMouseListener(listener);
        resultListModel = new ResultListModel();
        cacheKeywordList = new JList(resultListModel);
        cacheKeywordList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        cacheKeywordList.registerKeyboardAction(listListener,KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),JComponent.WHEN_FOCUSED);
        JComponent tipFrame = new JScrollPane(cacheKeywordList);
        tipFrame.setPreferredSize(new Dimension(240,100));
        System.out.println(keywordField.getWidth());
        popupMenu = new JPopupMenu();
        popupMenu.add(tipFrame);
        keywordField.addKeyListener(keyListener);
        
        Container pane  = getContentPane();
        pane.setLayout(new BorderLayout());
        JPanel searchPanel = new JPanel();
        searchPanel.add(keywordField);
        searchPanel.add(searchButton);
        pane.add(searchPanel,BorderLayout.NORTH);
        JTable table = new JTable();
        pane.add(table,BorderLayout.CENTER);
        
        setSize(500,300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private void doSearch(){
        String keyWord = keywordField.getText().trim();
        if(!keyWord.equals("")){
            cacheKeyword.add(keyWord);
        }
        keywordField.setText("");
        // do search .....
        new Thread(new Runnable(){

            public void run() {
                // searching...
            }
            
        }).start();
    }
    
    public void showMatch(){
        String keyWord = keywordField.getText().trim();
        if(keyWord.equals("")){
            popupMenu.setVisible(false);
            return;
        }
        Set<String> matchResult = getMatch(keyWord);
        if(matchResult==null || matchResult.size()<1){
            popupMenu.setVisible(false);
            return;
        }
        resultListModel.removeAllElements();
        for(String result:matchResult)
            resultListModel.addElement(result);
        popupMenu.show(keywordField,0, keywordField.getHeight());
        cacheKeywordList.setSelectedIndex(0);
        keywordField.requestFocus();
    }
    
    private Set<String> getMatch(String content){
        Set<String> tempSet = new HashSet<String>();
        for(String temp:cacheKeyword)
            if(temp.indexOf(content)!=-1)
                tempSet.add(temp);
        return tempSet;
    }
    

    public static void main(String[] args) {
        new IntelligentMatch();
    }

    
    
    
    /**inner class for answering event*/
    private class KeyDisposer extends KeyAdapter{
        public void keyReleased(KeyEvent e){
            if(e.getKeyCode()==27){
                popupMenu.setVisible(false);
            }else if(e.getKeyCode()==40){
                popupMenu.setVisible(true);
                cacheKeywordList.requestFocus();
                if(resultListModel.size()>=2)
                    cacheKeywordList.setSelectedIndex(1);
            }else if(e.getKeyCode()==38){
                popupMenu.setVisible(true);
                cacheKeywordList.requestFocus();
                cacheKeywordList.setSelectedIndex(resultListModel.size()-1);
            }else if(e.getKeyCode()==KeyEvent.VK_ENTER){
                
            }else {
                showMatch();
            }
        }
    }
    class Listener extends MouseAdapter implements ActionListener{

        public void actionPerformed(ActionEvent e){
            if(popupMenu.isShowing())
                listListener.actionPerformed(e);
            else
                executeClick();
            }
        public void mouseClicked(MouseEvent event){
            executeClick();
        }
        private void executeClick(){
            doSearch();
        }
    }
    private class ListListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
                
            keywordField.setText(cacheKeywordList.getSelectedValue().toString());
            popupMenu.setVisible(false);
            keywordField.requestFocus();
        }
    }
    private class ResultListModel extends DefaultListModel{
        ResultListModel(){
            //load the default cache keyword ---gavin
            for(String matchKeyword : matchResultSet)
                addElement(matchKeyword);
        }
    }

    


}